/**
 * Moondance UML modeling tool
 * 
 * @author Dascalu Laurentiu (dascalu.laurentziu@gmail.com).
 * 
 * This program is free software; you can redistribute it and
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package ro.pub.cs.se.moondance.xmi.converter;

import java.util.Iterator;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import ro.pub.cs.se.moondance.MoondanceData;
import ro.pub.cs.se.moondance.model.UMLBaseClass;
import ro.pub.cs.se.moondance.model.UMLClass;
import ro.pub.cs.se.moondance.model.UMLInterface;
import ro.pub.cs.se.moondance.model.UMLPackage;

public class UMLPackageXMI extends UMLBlockXMI {
	
	/** Example
	 * <packagedElement
	 *     xmi:type="uml:Package"
	 *     xmi:id="_fy2TsGcOEd67F_7YoWwK5g"
	 *     name="somePackage"/>
	 * May contain other packages, classes or interfaces
     */
	
	public UMLPackageXMI(String name, String id) {
		super(XMICommon.UML_PACKAGE, name, id);
	}
	
	public void serialize(Element root, Document document, UMLPackage myPackage,
			String name, boolean addConections) {
		if (root == null)
			return;
		List<UMLPackage> packets = myPackage.getContainedPackages();
		Node nodeIterator = root.getFirstChild();
		
		// Recursive add the contained packages in this package
		for (Iterator<UMLPackage> it = packets.iterator();
			it.hasNext();) {
			UMLPackage nextPackage = it.next();
			Element newRoot = null;
			
			if (!addConections) {
				nextPackage.setXMIid(MoondanceData.newRandomString());
				
				newRoot = document.createElement(XMICommon.PACKED_ELEMENT);
				newRoot.setAttribute(XMICommon.XMI_TYPE, XMICommon.UML_PACKAGE);
				newRoot.setAttribute(XMICommon.XMI_ID, nextPackage.getXMIid());
				newRoot.setAttribute(XMICommon.NAME, nextPackage.getName());
			} else {
				newRoot = (Element) nodeIterator;
				nodeIterator = root.getNextSibling();
			}
			
			serialize(newRoot, document, nextPackage, nextPackage.getName(), addConections);
			
			if (!addConections)
				root.appendChild(newRoot);
		}
		
		// Add classes and interfaces
		List<UMLBaseClass> baseClasses = myPackage.getContainedClasses();
		
		for (Iterator<UMLBaseClass> it = baseClasses.iterator();
			it.hasNext();) {
			UMLBaseClass currentClass = it.next();
			String newID;
			
			if (!addConections) {
				newID = MoondanceData.newRandomString();
				currentClass.setXMIid(newID);
			} else
				newID = currentClass.getXMIid();
			
			if (currentClass instanceof UMLClass) {
				UMLClassXMI xmiClass = new UMLClassXMI(currentClass.getName(), newID);
				xmiClass.serialize(root, document, (UMLClass) currentClass, addConections);
			} else if (currentClass instanceof UMLInterface) {
				UMLInterfaceXMI xmiInterface = new UMLInterfaceXMI(currentClass.getName(), newID);
				xmiInterface.serialize(root, document, (UMLInterface) currentClass, addConections);
			}
		}
	}
	
	public void serialize(Element root, Document document, UMLPackage myPackage,
			boolean addConnections) {
		
		if (!addConnections) {
			Element newRoot = document.createElement(XMICommon.PACKED_ELEMENT);
			newRoot.setAttribute(XMICommon.XMI_TYPE, XMICommon.UML_PACKAGE);
			newRoot.setAttribute(XMICommon.XMI_ID, id);
			newRoot.setAttribute(XMICommon.NAME, name);
			
			root.appendChild(newRoot);
		
			// Add packages, classes and interfaces
			serialize(newRoot, document, myPackage, name, addConnections);
		} else
			serialize(root, document, myPackage, name, addConnections);
	}
}
